package org.example.captcha.Controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.example.captcha.Server.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;

/**
 * 又前端发来创建一个验证码的请求，返回一个对应的图像验证码和这个验证码对于的cookie用来标识这个验证码
 * 自己通过session来储存这个图形码正确的答案
 * 前端返回一个验证码和对应的图像吗的cookie，那么我在通过Cookie来找到对应的验证码的正确答案
 */
@RequestMapping("captcha")
@CrossOrigin("http://127.0.0.1:5500")
@RestController
public class captchaController {
    @Autowired
    captchaServer captchaServer;

    //返回一个验证码图片和对应的cookie
    @RequestMapping("/getCode")
    public void getCaptcha(HttpServletResponse response, HttpSession session) {
        captchaServer.getCaptcha(response, session);
    }

    @RequestMapping("/verify")
    public boolean verifyCaptcha(HttpServletRequest request,String code,HttpSession session) {
        return captchaServer.verify(request,code,session);
    }
}
